
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _vue = _interopRequireDefault(require('vue'));

const _ImagePreview = _interopRequireDefault(require('./ImagePreview'));

const _utils = require('../utils');

let instance;
const defaultConfig = {
  loop: true,
  images: [],
  value: true,
  minZoom: 1 / 3,
  maxZoom: 3,
  className: '',
  onClose: null,
  onChange: null,
  showIndex: true,
  closeable: false,
  closeIcon: 'clear',
  asyncClose: false,
  startPosition: 0,
  swipeDuration: 500,
  showIndicators: false,
  closeOnPopstate: false,
  closeIconPosition: 'top-right',
};

const initInstance = function initInstance() {
  instance = new (_vue.default.extend(_ImagePreview.default))({
    el: document.createElement('div'),
  });
  document.body.appendChild(instance.$el);
  instance.$on('change', (index) => {
    if (instance.onChange) {
      instance.onChange(index);
    }
  });
  instance.$on('scale', (data) => {
    if (instance.onScale) {
      instance.onScale(data);
    }
  });
};

const ImagePreview = function ImagePreview(images, startPosition) {
  if (startPosition === void 0) {
    startPosition = 0;
  }

  /* istanbul ignore if */
  if (_utils.isServer) {
    return;
  }

  if (!instance) {
    initInstance();
  }

  const options = Array.isArray(images) ? {
    images,
    startPosition,
  } : images;
  (0, _extends2.default)(instance, defaultConfig, options);
  instance.$once('input', (show) => {
    instance.value = show;
  });
  instance.$once('closed', () => {
    instance.images = [];
  });

  if (options.onClose) {
    instance.$off('close');
    instance.$once('close', options.onClose);
  }

  return instance;
};

ImagePreview.Component = _ImagePreview.default;

ImagePreview.install = function () {
  _vue.default.use(_ImagePreview.default);
};

const _default = ImagePreview;
exports.default = _default;
